<template>
	<view class="wap">
		<navbar :parameter='parameter'></navbar>
		<checkbox-group @change="checkboxChange" v-if="patient_list.length>0">
			<block v-for="(item,index) in patient_list" :key="index">
				<view class="box acea-row row-between-wrapper" @click="choice(item)">
					<view class="acea-row row-top">
						<checkbox :value="item.id" :checked="item.isdefault" color="#398ADE" v-if="isChoice == 1">
						</checkbox>
						<view class="cont-box">
							<view class="name"> {{item.member_name}} </view>
							<view class="age-line">
								<text>{{item.age}}岁</text>
								<text>{{item.mobile}}</text>
							</view>
						</view>
					</view>
					<view @click.stop="edit(item.id)">
						<image src="/static/images/bianji.png" mode="widthFix" class="binaji"></image>
					</view>
				</view>
			</block>
		</checkbox-group>
		<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-else></u-empty>
		
		<view class="btn-box">
			<navigator url="" class="btn acea-row row-center-wrapper" url="/pages/user/add_patient" v-if="isAdd">
				<u-icon name="plus" color="#fff" size="16"></u-icon><text>添加就诊人</text>
			</navigator>
			<view class="btn acea-row row-center-wrapper" @click="del" v-else>删除</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				parameter: {
					'navbar': '1',
					'return': '1',
					'title': '就诊人管理',
					'color': true,
					'class': 'on'
				},
				radiovalue: '',
				patient_list: [],
				isChoice: 0, //是否从下单页进来
				idList: [],
				isAdd: true,

			};
		},
		onLoad(option) {
			console.log(option)
			this.memberLists()
			this.isChoice = option.isChoice;
		},
		onShow() {
			this.memberLists()
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
		
		},
		onShareTimeline(res) { //分享到朋友圈
		
		},

		methods: {
			async memberLists() {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/user/memberLists',
					data: {
						openid: uni.getStorageSync('openid')
					}
				})
				this.patient_list = res.data.data;
			},
			async delMember() {
				var that = this;
				var idList= this.idList.map(Number)
				console.log(idList)
				const res = await this.$myRequest({
					url: '/api/user/delMember',
					data: {
						openid: uni.getStorageSync('openid'),
						id: idList
					}
				})
				console.log(res.data)
				if (res.data.status == 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: '删除成功',
					})
					this.memberLists();
					this.isAdd = true;
				} else {
					this.$refs.uToast.show({
						type: 'error',
						message: res.data.msg,
					})
				}
			},
			choice(item) {
				if (this.isChoice == 0) {
					console.log(item);
					uni.$emit('patient', {
						patient: item
					});
					uni.navigateBack();
				} else {

				}

			},
			checkboxChange(e) {
				console.log('checkbox发生change事件，携带value值为：', e)
				var items = this.patient_list,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if (values.includes(item.value)) {
						this.$set(item, 'checked', true)
					} else {
						this.$set(item, 'checked', false)
					}
				}
				this.idList = e.detail.value;
				if (this.idList.length !== 0) {
					this.isAdd = false
				} else {
					this.isAdd = true
				}
			},
			edit(id) {
				uni.navigateTo({
					url: '/pages/user/add_patient?id=' + id
				});
			},
			del() {
				console.log(this.idList)
				this.delMember()
			}

		}
	}
</script>

<style lang="scss" scoped>
	.wap {
		padding: 30rpx;
	}

	.box {
		padding: 30rpx;
		background-color: #fff;
		padding: 30rpx;
		border-radius: 12rpx;
		margin-bottom: 30rpx;

	}

	.box .cont-box {
		padding-left: 10rpx;
	}

	.box .cont-box .age-line {
		font-size: 24rpx;
		padding-top: 16rpx;
		color: #A7A7A7;
	}

	.box .cont-box .age-line text {
		margin-right: 10rpx;
	}

	.box .binaji {
		width: 30rpx;
	}

	.btn-box {
		padding-top: 30rpx;
	}

	.btn-box .btn {
		width: 100%;
		height: 80rpx;
		border-radius: 10rpx;
		line-height: 80rpx;
		text-align: center;
		background-color: #347DFE;
		color: #fff;
		font-size: 32rpx;
	}

	.btn-box .btn text {
		margin-left: 10rpx;
	}
</style>
